unit oEditorPathsManager;

{
	created   5.4.2010
	author   	Miroslav Mintal
	purpose
}

interface

uses
   Windows, ComCtrls, Types, SysUtils, Dialogs, oManager, oProcessItem,
   OProcessObjectConstants, oPhysicalEntity, oFramePaths, oFormPaths,
   oPolygon, oFrameScene, oLevel, obstacle, ocoordinates, Vector,
   oLogicalEntity, oEntityTypeConstants, Path;

type
  EPathsManager = class(Exception)
  end;

  TEditorPathsManager = class(TManager)
    private
      aOldName: string;
      aFrame: TFramePaths;
      aEditorFrame : TfEditor;
      aForm: TformPaths;
      aEditing : boolean;
      procedure setComponent(paFrame: TFramePaths);
      procedure showLevel(paLevel : TLEvel);
    public
      constructor create(paMaster: TManager);

      function ProcessObject(paItem: TProcessItem):TProcessItem; override;
  end;

implementation

uses
  oProcessObjects;

{ TTreeManager }

function TEditorPathsManager.ProcessObject(paItem: TProcessItem): TProcessItem;
var
   suc, ret : Boolean;
   list : TProcessingList;
   name : string;
   item : TProcessItem;
   level : TLEvel;
begin
    ret := false;
    suc := false;
    list := nil;
    case paItem.Code of
      PO_EDITOR_PATHS_NEW_FRM : begin
        aForm := TformPaths.Create(nil);
        aForm.Show;
        setComponent(aForm.frame AS TFramePaths);
        aFrame.Manager := Self;
        aOldName := aFrame.txtName.Text;
        aEditorFrame := aForm.TfEditor1;
        aEditing := false;

        item := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ACTUAL_LEVEL, nil));
        level := (item.Data as TProcessingLevel).Level;
        if level <> nil then begin
          aEditorFrame.LevelSize := level.SizeX;
          aForm.init;
          showLevel(level);
        end;
        aForm.Show;
  //      form.FramePaths1.Init;
      end;
      PO_EDITOR_PATHS_LOAD_FRM : begin
        aForm := TformPaths.Create(nil);
        aForm.Show;
        setComponent(aForm.frame AS TFramePaths);
        aEditorFrame := aForm.TfEditor1;
        aEditing := true;
        aFrame.Manager := Self;
        aFrame.txtName.Text := (paItem.Data AS TString).String_;
        aOldName := aFrame.txtName.Text;
        item := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ACTUAL_LEVEL, nil));
        level := (item.Data as TProcessingLevel).Level;
        if level <> nil then
         begin
          aEditorFrame.LevelSize := level.SizeX;
          aForm.init;
          showLevel(level);
         end;
  //      form.FramePaths1.Init;
        aForm.Show;
      end;
      PO_EDITOR_PATHS_SAVE : begin // treba kontrolovat zmeny a podla toho insert, edit ...
        if aFrame.txtName.Text = '' then
          ShowMessage('Name can not be empty');
        if aOldName = '' then
          begin
            aMaster.ProcessObject(TProcessItem.create(PO_PATHS_INSERT_PATH, TString.Create(aFrame.txtName.Text)));
            aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PATHS, NIL));
            aMaster.ProcessObject(TProcessItem.create(PO_TREE_REFRESH_PATHS_SUBLAYER, nil));
          end
        else
          begin
          aMaster.ProcessObject(TProcessItem.create(PO_TREE_REFRESH_PATHS_SUBLAYER, nil));
//          aMaster.ProcessObject(TProcessItem.create(PO_PATHS_REMOVE_PATH, TString.Create(aOldName)));
//          aMaster.ProcessObject(TProcessItem.create(PO_PATHS_INSERT_PATH, TString.Create(aFrame.txtName.Text)));
          aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PATHS, NIL));
          end
      end;
      else raise EPathsManager.Create('not specified operation for TEditoPathsManager : ' + IntToStr(paItem.Code));
    end;

    if (ret) then begin
      if (suc) then
        Result := TProcessItem.create(PO_SUCCESS, nil)
      else
        Result := TProcessItem.create(PO_FAILURE, nil);
    end
    else
      Result := nil;
end;


procedure TEditorPathsManager.setComponent(paFrame: TFramePaths);
begin
  aFrame := paFrame;
end;

procedure TEditorPathsManager.showLevel(paLevel: TLEvel);
var
  phs : TPhysicalEntity;
  log : TLogicalEntity;
  obst : TPolygonObstacle;
  pp : TPathPoint;
  j : integer;
  coord : Tcoordinates;
begin
  // display level
for phs in paLevel.allObstacles do
 begin
  if phs is TPolygon then
   begin
    obst := TPolygonObstacle.Create;
    for j := 0 to TPolygon(phs).Points.Count - 1 do
     begin
      coord := TCoordinates(TPolygon(phs).Points[j]);
      obst.AddPoint(TVector3f.Create(coord.x, coord.y, 0));
     end;
    obst.ID := phs.ID;
    if phs.LogicalEntity = nil then
     obst.LogEntity:= false
    else obst.LogEntity:= true;
    coord:= TCoordinates(TPolygon(phs).Position);
    obst.Position:= TVector3f.Create(coord.X, coord.Y, 0);
    aEditorFrame.Obstacles.Add(obst);
    aEditorFrame.AllObstacles.Add(obst);
   end;
 end;

 for log in paLevel.AllLogicalEntities do
  begin
   if log.Shape is TPolygon then
    begin
     obst:= TPolygonObstacle.Create;
     case log.TypeLogic of
       ET_DOOR_TYPE: obst.Color.SetRGBA(0.66,0.78,2.23,0.5);
       ET_WALL_TYPE: obst.Color.SetRGBA(2.51,0.11,0.35,0.5);
       ET_STAIRS_TYPE: obst.Color.SetRGBA(0.28,1.64,0.85,0.5);
       ET_LIFT_TYPE: obst.Color.SetRGBA(2.42,2.37,0.28,0.5);
       ET_TURNIQUET_TYPE: obst.Color.SetRGBA(1.18,1.52,1.36,0.5);
       ET_SPAWN_TYPE: obst.Color.SetRGBA(2.43, 2.49, 0.21, 0.5);
       ET_SINK_TYPE: obst.Color.SetRGBA(0.65, 0.65, 0.65, 0.5);
       ET_WAYPOINT_TYPE: obst.Color.SetRGBA(0.64, 0, 0.64, 0.5);
     end;

   for j := 0 to TPolygon(log.Shape).Points.Count - 1 do
    begin
     coord:= TCoordinates(TPolygon(log.Shape).Points[j]);
     obst.AddPoint(TVector3f.Create(coord.X, coord.Y, 0));
    end;
   coord:= TCoordinates(TPolygon(log.Shape).Position);
   obst.Position:= TVector3f.Create(coord.X, coord.Y, 0);
   obst.Typ:= log.TypeLogic;
   obst.LogEntity:= true;
   obst.ID:= log.Id;
   aEditorFrame.LogObstacles.Add(obst);
   aEditorFrame.AllObstacles.Add(obst);
  end;
 end;
 aEditorFrame.Path.Reinitialize;

end;

constructor TEditorPathsManager.create(paMaster: TManager);
begin
  inherited Create(paMaster);
  aOldName := 'lol';
//  aFrame := nil;
end;

end.
